package com.kelvin.paopao.module.login.register.registerStep1;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kelvin.kelvinlib.utils.CommonUtil;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.login.register.registerStep2.RegisterStep2Activity;
import com.kelvin.paopao.widgets.CustomToolbar;
import com.kelvin.paopao.widgets.NolineColorSpan;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/5/7.
 */

public class RegisteStep1Activity extends BaseActivity {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.tv_info)
    TextView tvInfo;
    @BindView(R.id.tv_next_step)
    TextView tvNextStep;

    private String wechat_id;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_register_step1;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        tvInfo.setText(generateText());
        wechat_id = getIntent().getStringExtra("wechat_id");
        customToolbar.showBackIcon(this);
        customToolbar.setTitle("注册账号");

        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String user_id = etPhone.getText().toString().trim();
                String pwd = etPassword.getText().toString().trim();
                if (!TextUtils.isEmpty(user_id) && !TextUtils.isEmpty(pwd)) {
                    tvNextStep.setBackgroundResource(R.drawable.shape_login_btn);
                    tvNextStep.setTextColor(ContextCompat.getColor(RegisteStep1Activity.this, android.R.color.white));
                } else {
                    tvNextStep.setBackgroundResource(R.drawable.shape_login2_btn);
                    tvNextStep.setTextColor(ContextCompat.getColor(RegisteStep1Activity.this, R.color.text_color_dark));
                }
            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String user_id = etPhone.getText().toString().trim();
                String pwd = etPassword.getText().toString().trim();
                if (!TextUtils.isEmpty(user_id) && !TextUtils.isEmpty(pwd)) {
                    tvNextStep.setBackgroundResource(R.drawable.shape_login_btn);
                    tvNextStep.setTextColor(ContextCompat.getColor(RegisteStep1Activity.this, android.R.color.white));
                } else {
                    tvNextStep.setBackgroundResource(R.drawable.shape_login2_btn);
                    tvNextStep.setTextColor(ContextCompat.getColor(RegisteStep1Activity.this, R.color.text_color_dark));
                }
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick(R.id.tv_next_step)
    public void onClick() {
        if (TextUtils.isEmpty(etPhone.getText().toString().trim())) {
            ToastUtil.showToast(this, "请填写手机号码");
            return;
        }
        if (!CommonUtil.isPhoneNo(etPhone.getText().toString().trim())) {
            ToastUtil.showToast(this, "手机号码格式错误");
            return;
        }
        if (TextUtils.isEmpty(etPassword.getText().toString().trim())) {
            ToastUtil.showToast(this, "请填写密码");
            return;
        }
        RetrofitService.checkPhone(etPhone.getText().toString().trim()).subscribe(new Consumer<CommonListResponse>() {
            @Override
            public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                if ("成功".equals(commonListResponse.getReason())) {
                    RegisterStep2Activity.launch(RegisteStep1Activity.this, etPhone.getText().toString().trim(),
                            etPassword.getText().toString().trim(), wechat_id);
                } else {
                    ToastUtil.showToast(RegisteStep1Activity.this, "该手机已注册");
                }
            }
        });
    }

    /**
     * 生成可点击的text
     */
    private SpannableString generateText() {
        String text = "注册即表示同意泡泡有约《注册协议》";
        SpannableString spannableString = new SpannableString(text);
        spannableString.setSpan(new NolineColorSpan() {
            @Override
            public void onClick(View widget) {
                ToastUtil.showToast(RegisteStep1Activity.this, "进入协议页面");
            }
        }, 11, text.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.color_blue)), 11, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, RegisteStep1Activity.class);
        context.startActivity(intent);
    }

    public static void launch(Context context, String wechat_id) {
        Intent intent = new Intent(context, RegisteStep1Activity.class);
        intent.putExtra("wechat_id", wechat_id);
        context.startActivity(intent);
    }
}
